package org.ztr.yanai.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.ztr.yanai.blog.service.TagService;
import org.ztr.yanai.blog.utils.Result;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/11/22 10:00
 * @Description: 标签管理控制器，提供标签CRUD操作
 * @Version: 1.0
 */
@RestController
@RequestMapping("/tag")
@Tag(name = "标签管理", description = "标签相关的 API 接口")
public class TagController {

    private final TagService tagService;

    public TagController(TagService tagService) {
        this.tagService = tagService;
    }

    @PostMapping
    @Operation(summary = "创建标签", description = "根据传入的标签信息创建新标签")
    public Result<org.ztr.yanai.blog.entity.Tag> createTag(@RequestBody org.ztr.yanai.blog.entity.Tag tag) {
        boolean isSuccess = tagService.createTag(tag);
        return isSuccess ? Result.success(tag) : Result.error("创建失败");
    }

    @DeleteMapping("/{tagId}")
    @Operation(summary = "删除标签", description = "根据标签 ID 删除标签")
    @Parameter(name = "tagId", description = "标签 ID", required = true)
    public Result<?> deleteTag(@PathVariable Long tagId) {
        return tagService.deleteTagById(tagId) ? Result.success("删除标签成功！") : Result.error("删除失败");
    }

    @GetMapping("/{tagId}")
    @Operation(summary = "获取标签详情", description = "根据标签 ID 获取标签详细信息")
    @Parameter(name = "tagId", description = "标签 ID", required = true)
    public Result<org.ztr.yanai.blog.entity.Tag> getTagById(@PathVariable Long tagId) {
        return Result.success(tagService.getTagById(tagId));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询标签", description = "支持分页和关键字查询标签列表")
    @Parameters({
            @Parameter(name = "pageNum", description = "当前页码，默认为 1", example = "1"),
            @Parameter(name = "pageSize", description = "每页显示条数，默认为 10", example = "10"),
            @Parameter(name = "keyword", description = "搜索关键字，可选", example = "Java")
    })
    public Result<Page<org.ztr.yanai.blog.entity.Tag>> listTagsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword) {
        QueryWrapper<org.ztr.yanai.blog.entity.Tag> wrapper = new QueryWrapper<>();
        if (keyword != null && !keyword.isEmpty()) {
            wrapper.like("name", keyword)
                    .or().like("slug", keyword);
        }
        return Result.success(tagService.listTagsByPage(pageNum, pageSize, wrapper));
    }

    @PutMapping
    @Operation(summary = "更新标签", description = "根据传入的标签信息更新标签数据")
    public Result<?> updateTag(@RequestBody org.ztr.yanai.blog.entity.Tag tag) {
        return tagService.updateTag(tag) ? Result.success(tag) : Result.error("更新失败");
    }
}
